<?php

class noticiasActions extends sfActions {
        /* envia los datos de la tabla  EVENTOS  paginados en en una cantidad determinada */
    public function executeConversat_index(sfWebRequest $request){

        $c = new Criteria();
        $c->add(NoticiasPeer::ENABLED, 1);
        $c->add(NoticiasPeer::ID_CAT, 2);
        $c->add(NoticiasPeer::DATE, date("Y-m-d"), Criteria::GREATER_EQUAL);
        $c->addDescendingOrderByColumn(NoticiasPeer::DATE);

        // PA MAYOr EXPLICACION IR A   http://www.enmanosdenadie.com.ar/2009/06/symfony-paginacion-con-sfpropelpager/
        //sfPropelPayer('mensaje','cantidad de elemtos x pagina')
        $pager = new sfPropelPager('Noticias', sfConfig::get('app_max_per_page'));
        $pager->setCriteria($c);
        $pager->setPage($request->getParameter('page', 1));//setea la pagina en 1
        //preparala informacion para usarla en la vista
        $pager->init();
        $this->pager = $pager;
    }

    public function executeConversat_new(sfWebRequest $request){

    }

    public function executeConversat_new_save(sfWebRequest $request){
        $title = $request->getParameter('title');
        $summary = $request->getParameter('summary');
        $content = $request->getParameter('content');
        $date = $request->getParameter('date');
        $acceso=$request->getParameter('acceso');

        $orden=$request->getParameter('orden');
        $is_external = $request->getParameter('is_external', 0);//1 or empty
        $link = $request->getParameter('link');

        if($is_external!=''){ $is_external = 1; }
        if($date==""){
            $this->getUser()->setFlash('msg','Ingrese fecha');
            $this->redirect('@noticias_conversat_new');
        }

        $day = substr($date,0,2);
        $month = substr($date,3,2);
        $year = substr($date,6,4);
        $f_date = $year."-".$month."-".$day;

        $conversatorio = new Noticias();
        $conversatorio->setIdCat(2);
        $conversatorio->setTitle($title);
        $conversatorio->setSummary($summary);
        $conversatorio->setContent($content);
        $conversatorio->setDate($f_date);
        $conversatorio->setEnabled(1);
        $conversatorio->setAcceso($acceso);
        $conversatorio->setLink($link);
        $conversatorio->setIsExternal($is_external);
        $conversatorio->setOrden($orden);
        $conversatorio->save();

        $this->getUser()->setFlash('msg','Conversatorio creado satisfactoriamente');
        $this->redirect('@noticias_conversat');
    }

    public function executeConversat_edit(sfWebRequest $request){
        $id = $request->getParameter('id');
        $this->event = NoticiasPeer::retrieveByPK($id);
    }

    public function executeConversat_edit_save(sfWebRequest $request){
        $hdId = $request->getParameter('hdId');
        $title = $request->getParameter('title');
        $summary = $request->getParameter('summary');
        $content = $request->getParameter('content');
        $date = $request->getParameter('date');
        $acceso=$request->getParameter('acceso');

         $orden=$request->getParameter('orden');
        $is_external = $request->getParameter('is_external', 0);//1 or empty
        $link = $request->getParameter('link');

        if($is_external!=''){ $is_external = 1; }


        if($date==""){
            $this->getUser()->setFlash('msg','Ingrese fecha');
            $this->redirect('@noticias_conversat_edit?id='.$hdId);
        }

        $day = substr($date,0,2);
        $month = substr($date,3,2);
        $year = substr($date,6,4);
        $f_date = $year."-".$month."-".$day;

        $conversatorio = NoticiasPeer::retrieveByPK($hdId);
        $conversatorio->setTitle($title);
        $conversatorio->setSummary($summary);
        $conversatorio->setContent($content);
        $conversatorio->setDate($f_date);
        $conversatorio->setLink($link);
        $conversatorio->setIsExternal($is_external);
        $conversatorio->setOrden($orden);
        $conversatorio->setAcceso($acceso);
        $conversatorio->save();

        $this->getUser()->setFlash('msg','Conversatorio modificado satisfactoriamente');
        $this->redirect('@noticias_conversat');
    }

    public function executeConversat_delete(sfWebRequest $request){
        $id = $request->getParameter('id');
        $conversatorio = NoticiasPeer::retrieveByPK($id);//busca x llave primaria , es lo mismo que hacer el doselectone
        $conversatorio->setDeleted();
        


        $this->getUser()->setFlash('msg','Conversatorio eliminado satisfactoriamente');
        $this->redirect('@noticias_conversat');
    }







        /* envia los datos de la tabla  EVENTOS  paginados en en una cantidad determinada */
    public function executeEvento_index(sfWebRequest $request){

        $c = new Criteria();
        $c->add(NoticiasPeer::ENABLED, 1);
        $c->add(NoticiasPeer::ID_CAT, 1);
        $c->add(NoticiasPeer::DATE, date("Y-m-d"), Criteria::GREATER_EQUAL);
        $c->addDescendingOrderByColumn(NoticiasPeer::DATE);

        // PA MAYOr EXPLICACION IR A   http://www.enmanosdenadie.com.ar/2009/06/symfony-paginacion-con-sfpropelpager/
        //sfPropelPayer('mensaje','cantidad de elemtos x pagina')
        $pager = new sfPropelPager('Noticias', sfConfig::get('app_max_per_page'));
        $pager->setCriteria($c);
        $pager->setPage($request->getParameter('page', 1));//setea la pagina en 1
        //preparala informacion para usarla en la vista
        $pager->init();
        $this->pager = $pager;
    }

    public function executeEvento_new(sfWebRequest $request){

    }

    public function executeEvento_new_save(sfWebRequest $request){
        $title = $request->getParameter('title');
        $summary = $request->getParameter('summary');
        $content = $request->getParameter('content');
        $date = $request->getParameter('date');

        $orden=$request->getParameter('orden');
        $is_external = $request->getParameter('is_external', 0);//1 or empty
        $link = $request->getParameter('link');

        if($is_external!=''){ $is_external = 1; }

        if($date==""){
            $this->getUser()->setFlash('msg','Ingrese fecha');
            $this->redirect('@noticias_events_new');
        }

        $day = substr($date,0,2);
        $month = substr($date,3,2);
        $year = substr($date,6,4);
        $f_date = $year."-".$month."-".$day;

        
        $event = new Noticias();
        $event->setIdCat(1);
        $event->setTitle($title);
        $event->setSummary($summary);
        $event->setContent($content);
        $event->setDate($f_date);
        $event->setEnabled(1);
        $event->setLink($link);
        $event->setIsExternal($is_external);
        $event->setOrden($orden);
        $event->save();

        $this->getUser()->setFlash('msg','Evento creado satisfactoriamente');
        $this->redirect('@noticias_events');
    }

    public function executeEvento_edit(sfWebRequest $request){
        $id = $request->getParameter('id');
        $this->event = NoticiasPeer::retrieveByPK($id);
    }

    public function executeEvento_edit_save(sfWebRequest $request){
        $hdId = $request->getParameter('hdId');
        $title = $request->getParameter('title');
        $summary = $request->getParameter('summary');
        $content = $request->getParameter('content');
        $date = $request->getParameter('date');

        $orden=$request->getParameter('orden');
        $is_external = $request->getParameter('is_external', 0);//1 or empty
        $link = $request->getParameter('link');

        if($is_external!=''){ $is_external = 1; }


        if($date==""){
            $this->getUser()->setFlash('msg','Ingrese fecha');
            $this->redirect('@noticias_evento_edit?id='.$hdId);
        }

        $day = substr($date,0,2);
        $month = substr($date,3,2);
        $year = substr($date,6,4);
        $f_date = $year."-".$month."-".$day;

        $event = NoticiasPeer::retrieveByPK($hdId);
        $event->setTitle($title);
        $event->setSummary($summary);
        $event->setContent($content);
        $event->setDate($f_date);
        $event->setLink($link);
        $event->setIsExternal($is_external);
        $event->setOrden($orden);
        $event->save();

        $this->getUser()->setFlash('msg','Evento modificado satisfactoriamente');
        $this->redirect('@noticias_evento');
    }

    public function executeEvento_delete(sfWebRequest $request){
        $id = $request->getParameter('id');
        $event = NoticiasPeer::retrieveByPK($id);//busca x llave primaria , es lo mismo que hacer el doselectone
        $event->setDeleted();

        $this->getUser()->setFlash('msg','Evento eliminado satisfactoriamente');
        $this->redirect('@noticias_events');
    }
}